/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package window;

/**
 *
 * 1004. 最大连续1的个数 III
 * 给定一个由若干 0 和 1 组成的数组 A，我们最多可以将 K 个值从 0 变成 1 。
 *
 * 返回仅包含 1 的最长（连续）子数组的长度。
 *
 * @author z00611952
 * @since 2021/9/21
 */
public class leetcode1004_longestOnes {
    public static void main(String[] args) {
        Solution1 solution1 = new Solution1();
        int[] nums = {0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1};
        System.out.println(solution1.longestOnes(nums, 3));

    }

}
class Solution1 {
    public int longestOnes(int[] nums, int k) {
        int left = 0, right = 0;
        int len = Integer.MIN_VALUE;
        int valid =0;
        int zero = 0;

        while(right < nums.length){
            int num = nums[right];
            right++;
            if( num == 0){
                zero++;

            }
            while(zero > k){
                int lnum = nums[left];
                left++;
                if(lnum == 0){
                    zero--;
                }
            }
            len = Math.max(len, right - left);
        }

        return len == Integer.MIN_VALUE? 0 : len;

    }
}